<?php

/**
 * @BelongProject: flami_1
 * @Author: Zwc
 * @CreateTime: 2025-07-26 22:07:41
 * @Description: TODO
 */

namespace app\admin\service;


use app\admin\model\LangDataModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class LangDataService
{
    protected $model;
    public function __construct()
    {
        $this->model = new LangDataModel();
    }

    /**
     * 根据语言ID~获取语言的国际化详情
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLangData($langId): array
    {
        // 获取简体中文的语言
        $zhCnLangData = $this->getZhCnData();
        if (!$zhCnLangData) {
            return ['code' => -1, 'msg' => '简体中文语言不存在,请设置简体中文'];
        }
        $condition = [
            ['del_flag', '=', 0],
            ['lang_id', '=', $zhCnLangData['id']]
        ];
        $data = $this->model->where($condition)->select()->toArray();
        $resData = [];
        if ($langId != $zhCnLangData['id']) {
            foreach ($data as $val) {
                $resData[$val['type']] = [
                    'type' => $val['type'],
                    'title' => $val['value'],
                    'id' => 0,
                    'value' => '',
                ];
            }

            $condition = [
                ['del_flag', '=', 0],
                ['lang_id', '=', $langId ]
            ];
            $langData = $this->model->where($condition)->select()->toArray();
            foreach ($langData as $val) {
                if (isset($resData[$val['type']])) {
                    $resData[$val['type']]['id'] = $val['id'];
                    $resData[$val['type']]['value'] = $val['value'];
                }
            }
            $resData = array_values($resData);
        }else {
            // 获取所有的简体中文配置
            foreach ($data as $val) {
                $resData[] = [
                    'id' => $val['id'],
                    'type' => $val['type'],
                    'value' => $val['value'],
                ];
            }
        }
        return ['code' => 0, 'data' => $resData, 'msg' => '获取数据成功'];
    }

    /**
     * 保存
     * @param $params
     * @return array|void
     */
    public function saveLangData($params) {
        for ($i=0;$i < count($params['type']);$i++) {
            if (!empty($params['id'][$i])) {
                // 保存
                $upData = [
                    'type' => $params['type'][$i],
                    'value' => $params['value'][$i],
                    'lang_id' => $params['langId']
                ];
                $this->model->where(['id' => $params['id'][$i], 'del_flag' => 0])->data($upData)->save();
            }else {
                // 添加
                $insertData = [
                    'type' => $params['type'][$i],
                    'value' => $params['value'][$i],
                    'lang_id' => $params['langId']
                ];
                $this->model->insert($insertData);
            }
        }
        return ['code' => 0, 'msg' => '操作成功'];
    }

    /**
     * 获取简体中文
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    private function getZhCnData(): array
    {
        return CountryService::getDefaultLangData();
    }


}